﻿using System;
using System.Collections.Generic;


using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;

using System.Data.SqlClient;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Collections;

public partial class PromoteList : System.Web.UI.Page
{
    private string strconn;
    SqlCommand comm;
    SqlConnection conn;
    DataSet ds;
    SqlDataAdapter da;
    protected void Page_Load(object sender, EventArgs e)
    {
        strconn = "Server = localhost;database=dbTrain;trusted_connection=true";
        conn = new SqlConnection(strconn);
        ds = new DataSet();
        if (!IsPostBack)
            bindproducttogridview();
    }

    protected void bindproducttogridview()
    {
        try
        {
            if (conn.State == ConnectionState.Closed)
                conn.Open();
            comm = new SqlCommand("viPromote", conn);
            comm.CommandType = CommandType.StoredProcedure;
            da = new SqlDataAdapter(comm);
            da.Fill(ds);
            GridView1.DataSource = ds.Tables[0];
            GridView1.DataBind();
        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            conn.Close();
        }
    }
    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {

    }
    protected void btnAdd_Click(object sender, System.EventArgs e)
    {
        try
        {
            if (conn.State == ConnectionState.Open)
                conn.Close();
            else
                conn.Open();
            comm = new SqlCommand("AddPromote", conn);
            comm.CommandType = CommandType.StoredProcedure;


            comm.Parameters.Add("@PromoteName", SqlDbType.NVarChar).Value = txtPromoteName.Text; ;
            comm.Parameters.Add("@Discount", SqlDbType.Int).Value = txtDiscount.Text;
            
            comm.ExecuteNonQuery();
            bindproducttogridview();
        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            conn.Close();
        }
    }
    protected void GridView1_SelectedIndexChanged(object sender, System.EventArgs e)
    {
        
    }
    protected void GridView1_RowDeleting(object sender, System.Web.UI.WebControls.GridViewDeleteEventArgs e)
    {
        try
        {
            ds = new DataSet();
            if (conn.State == ConnectionState.Closed)
                conn.Open();

            GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
            int PromoteID = int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());

            comm = new SqlCommand("DeletePromote", conn);
            comm.CommandType = CommandType.StoredProcedure;
            comm.Parameters.Add("@PromoteID", SqlDbType.Int).Value = PromoteID;

            da = new SqlDataAdapter(comm);
            comm.ExecuteNonQuery();
            bindproducttogridview();

        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            conn.Close();
        }
    }
    protected void GridView1_RowEditing(object sender, System.Web.UI.WebControls.GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        bindproducttogridview();
    }


    protected void GridView1_RowCancelingEdit(object sender, System.Web.UI.WebControls.GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        bindproducttogridview();
    }
    protected void GridView1_RowUpdating(object sender, System.Web.UI.WebControls.GridViewUpdateEventArgs e)
    {
        if (conn.State == ConnectionState.Open)
            conn.Close();
        else
            conn.Open();
        int PromoteID = Convert.ToInt32(((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text);
        string PromoteName = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
        string Discount = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
        
        
        comm = new SqlCommand("UpdatePromote", conn);
        comm.CommandType = CommandType.StoredProcedure;
        comm.Parameters.Add("@PromoteID", SqlDbType.Int).Value = PromoteID;
        comm.Parameters.Add("@PromoteName", SqlDbType.NVarChar).Value = PromoteName;


        comm.Parameters.Add("@Discount", SqlDbType.Int).Value = Discount;

        
        
        comm.ExecuteNonQuery();
        //bindproducttogridview();

        GridView1.EditIndex = -1;
        bindproducttogridview();
        conn.Close();

    }

   

   
}